package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl  implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //--1-- 查当前购物车数据
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        List<ShoppingCart> myShoppingCart=shoppingCartMapper.list(shoppingCart);

        //--2--存在：数量加一      不存在：新增
        if(myShoppingCart !=null && myShoppingCart.size()>0){
            ShoppingCart cart1 = myShoppingCart.get(0);
            cart1.setNumber(cart1.getNumber()+1);  //数量加一
            shoppingCartMapper.update(cart1);    //更新
        }
        else {
            if (shoppingCartDTO.getDishId()!=null){
                Dish dish=dishMapper.getDishById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());    //名称
                shoppingCart.setImage(dish.getImage());  //图片
                shoppingCart.setAmount(dish.getPrice()); //价格
            }else {
                Setmeal setmeal=setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());    //名称
                shoppingCart.setImage(setmeal.getImage());  //图片
                shoppingCart.setAmount(setmeal.getPrice()); //价格
            }

            shoppingCart.setUserId(BaseContext.getCurrentId());  //用户id
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    //--2--展示
    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list(
                ShoppingCart.builder()
                        .userId(BaseContext.getCurrentId()).build());
    }

    //--3--清空
    @Override
    public void delete() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    //--4--删除购物车中一个商品
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //--1-- 查当前购物车数据
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        List<ShoppingCart> myShoppingCart=shoppingCartMapper.list(shoppingCart);

        //--2--数量大于一：减少一 个     等于1：删除
        if(myShoppingCart !=null && myShoppingCart.size()>0){
            ShoppingCart cart1 = myShoppingCart.get(0);    //当前选中的---购物车数据

            if (cart1.getNumber()>1){
                cart1.setNumber(cart1.getNumber()-1);     //减少一 个
                shoppingCartMapper.update(cart1);         //更新
            }else {
                shoppingCartMapper.delectById(cart1.getId());   //删除id唯一
            }
        }
    }
}
